package com.demo.test.other;

import com.alibaba.fastjson.JSON;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @author gy
 * @date 2023/10/26
 */
public class ResourceMonitorAlertTest2 {

    private static Integer ITEM_NUM = 50;

    @Test
    public void m1(){
        long startTime = System.currentTimeMillis();

        List<ResourceMonitorAlert> list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            ResourceMonitorAlert alert = new ResourceMonitorAlert();



            alert.setServerName("localhost");

            // 大于代表不正常
            if (i >= 45) {
                alert.setIsRed(true);
                //cpu告警状态 81-100
                alert.setCpuMin(getRandomNum(81,85));
                alert.setCpuAverage(getRandomNum(86,95));
                alert.setCpuMax(getRandomNum(96,99));

                //内存告警状态 81-100
                alert.setMemoryMin(getRandomNum(81,85));
                alert.setMemoryAverage(getRandomNum(86,95));
                alert.setMemoryMax(getRandomNum(96,98));

                //io告警状态 80000 - 90000
                alert.setIoMin(getRandomKB(80000,81000));
                alert.setIoAverage(getRandomKB(85000,86000));
                alert.setIoMax(getRandomKB(89000,90000));
            }else{
                alert.setIsRed(false);
                //cpu稳定状态 31-50
                alert.setCpuMin(getRandomNum(31,35));
                alert.setCpuAverage(getRandomNum(37,42));
                alert.setCpuMax(getRandomNum(45,50));

                //内存稳定状态 31-50
                alert.setMemoryMin(getRandomNum(31,35));
                alert.setMemoryAverage(getRandomNum(37,42));
                alert.setMemoryMax(getRandomNum(45,50));

                //io稳定状态 10000 - 60000
                alert.setIoMin(getRandomKB(10000,20000));
                alert.setIoAverage(getRandomKB(30000,40000));
                alert.setIoMax(getRandomKB(50000,60000));
            }

            //已用空间 60-75 之间
            alert.setUsedSpace(getRandomNum(60,75));
            alert.setRemainSpace((100 -
                    Integer.parseInt(alert.getUsedSpace().replace("%",""))) + "%");

            List<AlertDetail> detailList = new ArrayList<>();
            if (i >= 45) {
                int alertCount = getIntRandom(1, 6);
                // 告警次数就是详情列表数
                alert.setAlertNum(String.valueOf(alertCount));

                String virtualName = getVirtual();
                String description = getDescription();
                for (int j = 0; j < alertCount; j++) {
                    AlertDetail detail = new AlertDetail();
                    detail.setServerName("localhost");
                    detail.setVirtualName(virtualName);
                    detail.setAlgorithmFirm("榕亨");
                    detail.setEventNum(String.valueOf(getIntRandom(800, 1000)));
                    detail.setAlertDescription(description);
                    detailList.add(detail);
                }
                alert.setAlertDetails(detailList);
            }else{
                alert.setAlertNum(String.valueOf(0));
                alert.setAlertDetails(detailList);
            }
            list.add(alert);
        }
        List<ResourceMonitorAlert> newList =
                list.stream().sorted(Comparator.comparing(
                        ResourceMonitorAlert::getUsedSpace).reversed())
                .collect(Collectors.toList());

        System.out.println(JSON.toJSONString(newList));

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    /**
     * random.nextInt(max)表示生成[0,max]之间的随机数，然后对(max-min+1)取模。
     * 以生成[10,20]随机数为例，首先生成0-20的随机数，然后对(20-10+1)取模得到[0-10]之间的随机数，然后加上min=10，最后生成的
     * @param min 最小值
     * @param max 最大值
     * @return
     */
    private String getRandomNum(int min,int max) {
        Random random = new Random();
        int i = random.nextInt(max) % (max - min + 1) + min;
        return i + "%";
    }

    private String getRandomKB(int min, int max) {
        Random random = new Random();
        int i = random.nextInt(max) % (max - min + 1) + min;
        return i + " KB";
    }

    private Integer getIntRandom(int min,int max) {
        Random random = new Random();
        return random.nextInt(max) % (max - min + 1) + min;
    }

    private String getVirtual() {
        ArrayList<String> list = new ArrayList<>();
        list.add("视频质量诊断算法");
        list.add("路况检测算法");
        list.add("机动车检测算法");
        list.add("非机动车检测算法");
        list.add("行人检测算法");
        int min = 0;
        int max = 4;
        Random random = new Random();
        int i = random.nextInt(max) % (max - min + 1) + min;
        return list.get(i);
    }

    private String getDescription() {
        ArrayList<String> list = new ArrayList<>();
        list.add("CPU使用告警");
        list.add("内存使用告警");
        list.add("IO资源告警");
        int min = 0;
        int max = 2;
        Random random = new Random();
        int i = random.nextInt(max) % (max - min + 1) + min;
        return list.get(i);
    }
}
